package com.yssoft.service.inventory;

import com.yssoft.entity.inventory.TInstockApply;
import com.yssoft.entity.inventory.TInstockApplyDetail;
import com.yssoft.mapper.core.TDepartmentMapper;
import com.yssoft.mapper.inventory.TInstockApplyDetailMapper;
import com.yssoft.mapper.inventory.TInstockApplyMapper;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名称: InstockApplyService
 * @类描述: 入库申请service
 * @作者: 王永宁
 * @创建时间:2018年5月11日 下午4:31:58
 */
@Service
public class InstockApplyService {
    
    @Resource
    private TInstockApplyMapper instockApplyMapper;
    @Resource
    private TInstockApplyDetailMapper instockApplyDetailMapper;
    @Resource
    private TDepartmentMapper deptMapper;
    /**
     * @方法名称: findPage
     * @功能描述: 获取入库申请列表
     * @作者: 王永宁
     * @创建时间:2018年5月11日 下午4:39:40
     * @param page
     * @param limit
     * @param companyId
     * @param createTime
     * @param productName
     * @param stat
     * @return List<Map<String,Object>>
     */
    public List<Map<String, Object>> findPage(int page, int limit, Integer companyId,
                                              String createTime, String stat) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("createTime", createTime);
        param.put("inspStat", stat.equals("2") ? null: stat.equals("3") ? "1" :"0");
//        if("3".equals(stat)){
//        	param.put("inspStat",1);
//        }
        if (!"3".equals(stat)) {
            param.put("stat", stat);
        }
        param.put("companyId", companyId);
        int start = ((page - 1) * limit);
        int end = limit;
        param.put("start", start);
        param.put("end", end);
        List<Map<String, Object>> labelApplyList = instockApplyMapper.selectInstockApply(param);
        for (Map<String, Object> map : labelApplyList) {
            if ("3".equals(stat)) {
                int count = instockApplyMapper.selectNonCount(Integer.valueOf(map.get("id").toString()));
                map.put("stat", "3");
                map.put("non", count);
            }
        }
        return labelApplyList;
    }
	
  /**
  * @方法名称: deleteInstockApply
  * @功能描述: 删除入库申请
  * @作者: 王永宁
  * @创建时间:2018年5月11日 下午11:49:55
  * @param id
  * @param entity void
  * */
	public void deleteInstockApply(Integer id) {
		TInstockApply stockApply = instockApplyMapper.selectByPrimaryKey(id);
		stockApply.setDeleteFlag("1");
		instockApplyMapper.updateByPrimaryKeySelective(stockApply);
		
	}

    /**
     * @方法名称: saveInstockApply
     * @功能描述: 保存入库申请
     * @作者: 王永宁
     * @创建时间:2018年5月11日 下午11:33:38
     * @param instockApply 入库申请实体
     * @param warehouseId 仓库id
     * @param pId 商品id
     * @return Object
     */
	@Transactional
	public void saveInstockApply(TInstockApply instockApply, Integer warehouseId,Integer tpId,Integer pId) {
		if(instockApply.getId()!=null){
			instockApply.settWarehouseId(warehouseId);
			instockApplyMapper.updateByPrimaryKeySelective(instockApply);
			TInstockApplyDetail detail=new TInstockApplyDetail();
			detail.settInstockAppId(instockApply.getId());
			TInstockApplyDetail instockApplyDetaile = instockApplyDetailMapper.selectOne(detail);
			if(tpId==1){
				instockApplyDetaile.setpType("1");
				instockApplyDetaile.settSgId(null);
				instockApplyDetaile.settPId(pId);
		}else{
			instockApplyDetaile.setpType("2");
			instockApplyDetaile.settSgId(pId);
			instockApplyDetaile.settPId(null);
		}
			instockApplyDetaile.setQuantity(instockApply.getQuantity());
			instockApplyDetaile.settSourceId(instockApply.gettSourceId());
			instockApplyDetaile.settWarehouseId(warehouseId);
			instockApplyDetailMapper.updateByPrimaryKey(instockApplyDetaile);
			
			
		}else{
		String code = "RKS" + Calendar.getInstance().getTimeInMillis();
		instockApply.setDeleteFlag("0");
		instockApply.setCode(code);
		instockApply.setCreateTime(new Date());
		instockApply.setSourceType("1");
		instockApply.setInspStat("0");
		instockApply.setStat("1");
		instockApply.settWarehouseId(warehouseId);
		instockApplyMapper.insertSelective(instockApply);
		
		Integer id = instockApply.getId();
		TInstockApplyDetail instockApplyDetail=new TInstockApplyDetail();
		instockApplyDetail.setSourceType("1");
		if(tpId==1){
			instockApplyDetail.setpType("1");
			instockApplyDetail.settPId(pId);
		}else{
			instockApplyDetail.setpType("2");
			instockApplyDetail.settSgId(pId);
		}
		instockApplyDetail.setQuantity(instockApply.getQuantity());
		instockApplyDetail.setCreatorId(instockApply.getCreatorId());
		instockApplyDetail.setCreatorName(instockApply.getCreatorName());
		instockApplyDetail.setCreateTime(new Date());
		instockApplyDetail.setDeptId(instockApply.getDeptId());
		instockApplyDetail.setDeptName(instockApply.getDeptName());
		instockApplyDetail.settEId(instockApply.gettEId());
		instockApplyDetail.settSourceId(instockApply.gettSourceId());
		instockApplyDetail.settWarehouseId(warehouseId);
		instockApplyDetail.setDeleteFlag("0");
		instockApplyDetail.setStat("1");
		instockApplyDetail.settInstockAppId(id);
		instockApplyDetailMapper.insertSelective(instockApplyDetail);
		}
	}
	
    /**
     * @方法名称: getOneById
     * @功能描述: 根据id获取入库申请单
     * @作者: 王永宁
     * @创建时间:2018年5月18日 下午7:33:38
     * @param id 入库申请id
     * @return Object
     */
	public Map<String, Object> getOneById(Integer id) {
		return instockApplyMapper.getOneById(id);
		
	}
  
}


